package user

import (
	"gitee.com/cooge/impush/core"
	"gitee.com/cooge/impush/message"
)

func AddUser(userId string, http *HttpConn) {
	core.AddUser(userId, http)
}
func DeleteUser(userId string, http *HttpConn) {
	core.DeleteUser(userId, http)
}

func IsMessgeCache(userId string, message *message.Message) bool {
	u := core.GetUser(userId)
	if u != nil {
		lock := u.GetLock()
		lock.Lock()
		for e := u.Front(); e != nil; e = e.Next() {
			hu, ok := e.Value.(*HttpConn)
			if ok {
				if !hu.IsBreak() {
					lock.Unlock()
					return false
				}
			}
		}
		u.AddM(message)
		lock.Unlock()
		return true
	}
	return false
}
func GetMessageCache(userId string) []*message.Message {
	u := core.GetDelMS(userId)
	if u != nil && len(u) > 0 {
		data := make([]*message.Message, len(u))
		for i := 0; i < len(u); i++ {
			m, ok := u[i].(*message.Message)
			if ok {
				data[i] = m
			}
		}
		return data
	}
	return nil
}
